package com.YxyBackMange.Controller.PhoneSmsCode;

import com.YxyBackMange.Constant.DictTypeConstant;
import com.YxyBackMange.Util.Sendsms;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @outhor 路♥克
 * 2017-12-09 18:14
 */
@Controller
@RequestMapping("/ValidateCode")
public class ValidateCodeController {

    @ResponseBody
    @RequestMapping(value = "/send",method = RequestMethod.GET)
    public Boolean send(String phone, HttpSession session){
        Object phoneTime = session.getAttribute(phone);
        if (phoneTime!=null){
            long nowtime = System.currentTimeMillis();
            long bettweenTime = (nowtime- Long.parseLong(phoneTime.toString()))/(1000 * 60);
            if(bettweenTime >= DictTypeConstant.BETTWUEENTIME){
                Integer code = Sendsms.send(phone);
                System.out.println("phonecode:"+code);
                session.setAttribute(phone+"code",code);
                session.setAttribute(phone,System.currentTimeMillis());
                return true;
            }else{
                return false;
            }
        }else{
            Integer code = Sendsms.send(phone);
            System.out.println("phonecode:"+code);
            session.setAttribute(phone+"code",code);
            session.setAttribute(phone,System.currentTimeMillis());
            return true;
        }
    }
}
